using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Sauce.Internal.Analytics;
using Voodoo.Sauce.Internal.Extension;

namespace Voodoo.Sauce.Debugger
{
	public class EventConsoleInformationScreen : Screen
	{
		private const string DEFAULT_HEADER_FIELDS = "other header fields";

		private const string EVENT_DATE_FORMAT = "dd/MM/yyyy HH:mm:ss";

		[Header("Console Information")]
		[SerializeField]
		private EventConsoleErrorScreen errorScreen;

		[SerializeField]
		private EventInformationList eventInformationListPrefab;

		[SerializeField]
		private EventInformationLineItem eventInformationLineItemPrefab;

		[SerializeField]
		private Text eventName;

		[SerializeField]
		private Text eventDate;

		[SerializeField]
		private GameObject eventSendingStatus;

		[SerializeField]
		private GameObject eventSentStatus;

		[SerializeField]
		private GameObject eventErrorStatus;

		[SerializeField]
		private Transform eventInformationContainer;

		[SerializeField]
		private Button eventCopyJson;

		[SerializeField]
		private Button eventSeeError;

		private readonly Dictionary<string, EventInformationList> _eventInformationListDictionary = new Dictionary<string, EventInformationList>();

		private readonly Stack<EventInformationLineItem> _eventInformationLinePool = new Stack<EventInformationLineItem>();

		private void OnEnable()
		{
			RefreshInformationSize();
		}

		private EventInformationList CreateEventInformationList(string title)
		{
			EventInformationList eventInformationList = Object.Instantiate(eventInformationListPrefab, eventInformationContainer);
			_eventInformationListDictionary.Add(title, eventInformationList);
			eventInformationList.Initialize(title);
			return eventInformationList;
		}

		private void CreateEventInformationLine(KeyValuePair<string, object> information, Transform parent)
		{
			EventInformationLineItem eventInformationLineItem;
			if (_eventInformationLinePool.Count == 0)
			{
				eventInformationLineItem = Object.Instantiate(eventInformationLineItemPrefab, parent);
			}
			else
			{
				eventInformationLineItem = _eventInformationLinePool.Pop();
				eventInformationLineItem.transform.SetParent(parent);
				eventInformationLineItem.gameObject.SetActive(value: true);
			}
			eventInformationLineItem.UpdateData(information);
		}

		private void UpdateEventInformationHeader(DebugAnalyticsLog log)
		{
			eventName.text = log.EventName.BoldText();
			eventCopyJson.onClick.RemoveAllListeners();
			eventCopyJson.onClick.AddListener(delegate
			{
				CopyEventJson(log);
			});
			eventDate.text = log.Timestamp.ToString("dd/MM/yyyy HH:mm:ss").BoldText();
			switch (log.StateEnum)
			{
			case DebugAnalyticsStateEnum.ForwardedTo3rdParty:
				eventSendingStatus.SetActive(value: true);
				eventSentStatus.SetActive(value: false);
				eventErrorStatus.SetActive(value: false);
				break;
			case DebugAnalyticsStateEnum.Sent:
				eventSendingStatus.SetActive(value: false);
				eventSentStatus.SetActive(value: true);
				eventErrorStatus.SetActive(value: false);
				break;
			case DebugAnalyticsStateEnum.ErrorSending:
			case DebugAnalyticsStateEnum.SentButErrorFromServer:
			case DebugAnalyticsStateEnum.Error:
				eventSendingStatus.SetActive(value: false);
				eventSentStatus.SetActive(value: false);
				eventErrorStatus.SetActive(value: true);
				eventSeeError.onClick.RemoveAllListeners();
				eventSeeError.onClick.AddListener(delegate
				{
					OnErrorClicked(log);
				});
				break;
			}
		}

		private void UpdateEventInformationBody(DebugAnalyticsLog log)
		{
			EventInformationList eventInformationList = (_eventInformationListDictionary.ContainsKey("other header fields") ? _eventInformationListDictionary["other header fields"] : CreateEventInformationList("other header fields"));
			foreach (KeyValuePair<string, object> parameter in log.Parameters)
			{
				if (parameter.Value is Dictionary<string, object> dictionary)
				{
					EventInformationList eventInformationList2 = (_eventInformationListDictionary.ContainsKey(parameter.Key) ? _eventInformationListDictionary[parameter.Key] : CreateEventInformationList(parameter.Key));
					foreach (KeyValuePair<string, object> item in dictionary)
					{
						CreateEventInformationLine(item, eventInformationList2.GetContainer);
					}
					eventInformationList2.gameObject.SetActive(value: true);
				}
				else
				{
					CreateEventInformationLine(parameter, eventInformationList.GetContainer);
					eventInformationList.gameObject.SetActive(value: true);
				}
				eventInformationList.transform.SetAsLastSibling();
			}
		}

		private void OnErrorClicked(DebugAnalyticsLog log)
		{
			errorScreen.ShowErrorMessage(log);
		}

		public void ShowEventDescription(DebugAnalyticsLog log)
		{
			UpdateEventInformationHeader(log);
			CleanEventInformationBody();
			UpdateEventInformationBody(log);
			RefreshInformationSize();
		}

		private void CleanEventInformationBody()
		{
			foreach (KeyValuePair<string, EventInformationList> item in _eventInformationListDictionary)
			{
				foreach (Transform item2 in item.Value.GetContainer)
				{
					item2.gameObject.SetActive(value: false);
					_eventInformationLinePool.Push(item2.GetComponent<EventInformationLineItem>());
				}
				item.Value.gameObject.SetActive(value: false);
			}
		}

		private void RefreshInformationSize()
		{
			foreach (KeyValuePair<string, EventInformationList> item in _eventInformationListDictionary.Where((KeyValuePair<string, EventInformationList> keyValue) => keyValue.Value.gameObject.activeSelf))
			{
				item.Value.GetContainer.RefreshHierarchySize();
			}
		}

		private static void CopyEventJson(DebugAnalyticsLog log)
		{
			log.Parameters.ToJson().CopyToClipboard();
		}
	}
}
